<?php

require_once 'vendor/autoload.php';
use JPush\Model as M;
use JPush\JPushClient;
use JPush\JPushLog;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use JPush\Exception\APIConnectionException;
use JPush\Exception\APIRequestException;


	//检测session
	function checkSession(){
		$adminLogin = session('adminLogin');
		if(empty($adminLogin)){
			return false;
		}else{
			return true;
		}
	}

	//随机数字
	function getRndNumber($length) {
		$pattern = '1234567890';
		for ($i = 0; $i < $length; $i++) {
			$key .= $pattern{mt_rand(0, 9)};
		}
		return $key;
	}
	//随机字符串
	function getRndString($length) {
		$pattern = '1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
		$key = '';
		for ($i = 0; $i < $length; $i++) {
			$key .= $pattern{mt_rand(0, 62)};
		}
		return $key;
	}

	//color
	function getColor($color){
		switch ($color)
		{
		case "红色":
		  return "#ff283c";
		  break;
		case "蓝色":
		  return "#2874ff";
		  break;
		case "黑色":
		  return "#000000";
		  break;
		case "白色":
		  return "#ffffff";
		  break;
		case "黄色":
		  return "#8dff28";
		  break;
		case "绿色":
		  return "#29b96d";
		  break;
		case "粉色":
		  return "#db9ca5";
		  break;
		case "紫色":
		  return "#9823e2";
		  break;
		case "银灰色":
		  return "#c6c6c6";
		  break;
		case "深灰色":
		  return "#707070";
		  break;
		case "香槟色":
		  return "#cac58f";
		  break;
		case "金色":
		  return "#e9bf3f";
		  break;
		default:
		  return "#000000";
		}
	}

	//API-GUID
	function getGuid(){
		$guid = md5(time().C('MD5_KEY').getRndString(6));
		return $guid;
	}

	//生成代金券序列号
	function getCardCode(){
		$str = date('Y-m-d H:i:s').'uz'.getRndNumber(4);
		$key = md5($str);
		$code = strtoupper(substr($key,1,8).getRndString(4));
		return $code;
	}

	//代金券金额随机
	function getCardMoeny(){
		$pattern = '23456';
		for ($i = 0; $i < 1; $i++) {
			$key .= $pattern{mt_rand(0, 4)};
		}
		return $key;
	}

	
	// 给全部用户推送消息
	function message_push($message,$arr){
		$a = array();
		$app_key='5ce12c56b0dc32cf27cc73d1';
		$master_secret = '6a40a668a68986b4741994d2';
		JPushLog::setLogHandlers(array(new StreamHandler('jpush.log', Logger::DEBUG)));
		$client = new JPushClient($app_key, $master_secret);
		try {
		    $result = $client->push()
		    	->setOptions(M\options(1314520, 600, null, true, 0))
		        ->setPlatform(M\all)
		        ->setAudience(M\all)
		        // ->setNotification(M\notification($message))
		        ->setNotification(M\notification('Hi, JPush',
		            M\android($message, '优赞通知', 1,$a),
		            M\ios($message, "happy", "+1", true,$arr, "Ios8 Category")
		        ))
		        ->send();
		    return array('httpCode'=>'200','reson'=>"消息推送成功");
		} catch (APIRequestException $e) {
			return array('httpCode'=>$e->httpCode,'reson'=>$e->message);
		} catch (APIConnectionException $e) {
			return array('httpCode'=>$e->httpCode,'reson'=>$e->getMessage);
		}
	}


	// 推送给一个或多个标签tag
	function tag_push($tags,$message){
		$app_key='5ce12c56b0dc32cf27cc73d1';
		$master_secret = '6a40a668a68986b4741994d2';
		JPushLog::setLogHandlers(array(new StreamHandler('jpush.log', Logger::DEBUG)));
		$client = new JPushClient($app_key, $master_secret);
		try {
		    $result = $client->push()
		    	->setOptions(M\options(1314520, 600, null, true, 0))
		        ->setPlatform(M\Platform('android', 'ios'))
		        ->setAudience(M\Audience(M\Tag($tags)))
		        ->setNotification(M\notification('Hi, JPush',
		            M\android($message, 'Message Title', 1, array("key1"=>"value1", "key2"=>"value2")),
		            M\ios($message, "happy", "+1", true, array("key1"=>"value1", "key2"=>"value2"), "Ios8 Category")
		        ))
		        ->setMessage(M\message('Message Content', 'Message Title', 'Message Type', array("key1"=>"value1", "key2"=>"value2")))
		        ->send();
		    return array('httpCode'=>'200','reson'=>'指定标签用户消息推送成功');
		} catch (APIRequestException $e) {
		   return array('httpCode'=>$e->httpCode,'reson'=>$e->message);
		} catch (APIConnectionException $e) {
		   return array('httpCode'=>$e->httpCode,'reson'=>$e->getMessage);
		}
	}


	// 推送给一个或多个别名alias数组
	function alias_push($alias,$message,$arr){
		$a = array();
		$app_key='5ce12c56b0dc32cf27cc73d1';
		$master_secret = '6a40a668a68986b4741994d2';
		JPushLog::setLogHandlers(array(new StreamHandler('jpush.log', Logger::DEBUG)));
		$client = new JPushClient($app_key, $master_secret);
		try {
		    $result = $client->push()
		        ->setPlatform(M\all)
		        ->setAudience(M\audience(M\alias($alias)))
		        ->setOptions(M\options(1314520, 86400, null, true, 0))
		        // ->setNotification(M\notification($message))
                ->setNotification(M\notification('Hi, JPush',
                    M\android($message, '优赞通知', 1,$a),
                    M\ios($message, "happy", "+1", true,$arr , "Ios8 Category")
                ))
		        ->send();
		    return array('httpCode'=>'200','reson'=>'指定用户消息推送成功');
		} catch (APIRequestException $e) {
		    return array('httpCode'=>$e->httpCode,'reson'=>$e->message);
		} catch (APIConnectionException $e) {
		    return array('httpCode'=>$e->httpCode,'reson'=>$e->getMessage);
		}
	}

	// banner改变 推送
		function alias_push_banner($alias,$message,$arr){
			$app_key='5ce12c56b0dc32cf27cc73d1';
			$master_secret = '6a40a668a68986b4741994d2';
			$a = array();
			JPushLog::setLogHandlers(array(new StreamHandler('jpush.log', Logger::DEBUG)));
			$client = new JPushClient($app_key, $master_secret);
			try {
			    $result = $client->push()
			        ->setPlatform(M\all)
			        ->setAudience(M\audience(M\alias($alias)))
			        ->setOptions(M\options(1314520, 86400, null, true, 0))
			        // ->setNotification(M\notification($message))
	                ->setNotification(M\notification('Hi, JPush',
	                    M\android($message, '优赞通知', 1,$a),
	                    M\ios($message, "happy", "+1", true,$arr , "Ios8 Category")
	                ))
			        ->send();
			    return array('httpCode'=>'200','reson'=>'指定用户消息推送成功');
			} catch (APIRequestException $e) {
			    return array('httpCode'=>$e->httpCode,'reson'=>$e->message);
			} catch (APIConnectionException $e) {
			    return array('httpCode'=>$e->httpCode,'reson'=>$e->getMessage);
			}
		}

		// 推送跳转到广告页面
		function alias_push_to_web($alias,$message,$arr){
			$a = array();
			$app_key='5ce12c56b0dc32cf27cc73d1';
			$master_secret = '6a40a668a68986b4741994d2';
			JPushLog::setLogHandlers(array(new StreamHandler('jpush.log', Logger::DEBUG)));
			$client = new JPushClient($app_key, $master_secret);
			try {
			    $result = $client->push()
			        ->setPlatform(M\all)
			        ->setAudience(M\audience(M\alias($alias)))
			        ->setOptions(M\options(1314520, 86400, null, true, 0))
			        // ->setNotification(M\notification($message))
	                ->setNotification(M\notification('Hi, JPush',
	                    M\android($message, '优赞通知', 1,$a),
	                    M\ios($message, "happy", "+1", true,$arr, "Ios8 Category")
	                ))
			        ->send();
			    return array('httpCode'=>'200','reson'=>'指定用户消息推送成功');
			} catch (APIRequestException $e) {
			    return array('httpCode'=>$e->httpCode,'reson'=>$e->message);
			} catch (APIConnectionException $e) {
			    return array('httpCode'=>$e->httpCode,'reson'=>$e->getMessage);
			}
		}


	// 给全部用户推送消息
	function message_push1($message){
		$app_key='71b3f90d96169cf62480689e';
		$master_secret = '9ff29387daada2893788df1f';
		JPushLog::setLogHandlers(array(new StreamHandler('jpush.log', Logger::DEBUG)));
		$client = new JPushClient($app_key, $master_secret);
		try {
		    $result = $client->push()
		    	->setOptions(M\options(1314520, 86400, null, true, 0))
		        ->setPlatform(M\all)
		        ->setAudience(M\all)
		        ->setNotification(M\notification($message))
		        ->printJSON()
		        ->send();
		    return array('httpCode'=>'200','reson'=>"消息推送成功");
		} catch (APIRequestException $e) {
			return array('httpCode'=>$e->httpCode,'reson'=>$e->message);
		} catch (APIConnectionException $e) {
			return array('httpCode'=>$e->httpCode,'reson'=>$e->getMessage);
		}
	}


	// 推送给一个或多个标签tag
	function tag_push1($tags,$message){
		$app_key='71b3f90d96169cf62480689e';
		$master_secret = '9ff29387daada2893788df1f';
		JPushLog::setLogHandlers(array(new StreamHandler('jpush.log', Logger::DEBUG)));
		$client = new JPushClient($app_key, $master_secret);
		try {
		    $result = $client->push()
		    	->setOptions(M\options(1314520, 600, null, true, 0))
		        ->setPlatform(M\Platform('android', 'ios'))
		        ->setAudience(M\Audience(M\Tag($tags)))
		        ->setNotification(M\notification('Hi, JPush',
		            M\android($message, 'Message Title', 1, array("key1"=>"value1", "key2"=>"value2")),
		            M\ios($message, "happy", "+1", true, array("key1"=>"value1", "key2"=>"value2"), "Ios8 Category")
		        ))
		        ->setMessage(M\message('Message Content', 'Message Title', 'Message Type', array("key1"=>"value1", "key2"=>"value2")))
		        ->send();
		    return array('httpCode'=>'200','reson'=>'指定标签用户消息推送成功');
		} catch (APIRequestException $e) {
		   return array('httpCode'=>$e->httpCode,'reson'=>$e->message);
		} catch (APIConnectionException $e) {
		   return array('httpCode'=>$e->httpCode,'reson'=>$e->getMessage);
		}
	}


	// 推送给一个或多个别名alias数组
	function alias_push1($alias,$message,$arr,$type){
        if($type == 1){     //用户
            $app_key='5ce12c56b0dc32cf27cc73d1';
            $master_secret = '6a40a668a68986b4741994d2';
        }elseif($type == 2){        //工作人员
            $app_key='71b3f90d96169cf62480689e';
            $master_secret = '9ff29387daada2893788df1f';
        }

		JPushLog::setLogHandlers(array(new StreamHandler('jpush.log', Logger::DEBUG)));
		$client = new JPushClient($app_key, $master_secret);
		try {
		    $result = $client->push()
		        ->setPlatform(M\all)
		        ->setOptions(M\options(1314520, 600, null, true, 0))
		        ->setAudience(M\audience(M\alias($alias)))
                ->setNotification(M\notification('Hi, JPush',
                    M\android($message, '优赞通知', 1,$arr),
                    M\ios($message, "happy", "+1", true,$arr , "Ios8 Category")
                ))
		        ->send();
		    return array('httpCode'=>'200','reson'=>'指定用户消息推送成功');
		} catch (APIRequestException $e) {
		    return array('httpCode'=>$e->httpCode,'reson'=>$e->message);
		} catch (APIConnectionException $e) {
		    return array('httpCode'=>$e->httpCode,'reson'=>$e->getMessage);
		}
	}